package com.qst.crop.service.impl;

import com.qst.crop.dao.AddressDao;
import com.qst.crop.entity.Address;
import com.qst.crop.security.entity.JwtUser;
import com.qst.crop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Override
    public int add(Address address){
        return addressDao.add(address);
    }

    @Override
    public List<Address> selectByOwnName(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        return addressDao.selectByOwnName(username);
    }



    @Override
    public int updateByPrimaryKey(Address address){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        address.setOwnName(username);



        return addressDao.updateByPrimaryKey(address);
    }


    @Override
    public int deleteByPrimaryKey(Integer id){
        return addressDao.deleteByPrimaryKey(id);
    }

    @Override
    public Address selectDefaultByOwnName(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        return addressDao.selectDefaultByOwnName(username);
    }


    @Override
    public int setDefaultAddress(String username){
        return addressDao.setDefaultAddress(username);
    }
}
